import requests
import pymysql
import datetime

class NewsParse:
    docurls = []

    def __init__(self, parseNewPage, parseNewDetail):
        '''
        :param parseNewPage: 解析一个页面的新闻列表
        :param parseNewDetail: 解析新闻正文的函数
        '''
        self._getPage = parseNewPage
        self._getNewDetail = parseNewDetail

    def parseHtmlPageLinks(self, pageUrl):
        newsList = []
        itemsUrl = self._getPage(pageUrl)
        for item in itemsUrl:
            if not (item['docurl'] in self.docurls):
                self.docurls.append(item['docurl'])
                news = self._getNewDetail(item['docurl'])
                if news != None:
                    news['imgurl'] = item['imgurl']
                    newsList.append(news)
        return newsList

    def parseJsonPageLinks(self, pageurl):
        '''

        :param pageurl: 页面链接
        :return:
        '''
        newsList = []
        jsonstring, docurl, imgurl = self._getPage(pageurl)
        if jsonstring == None:
            return newsList
        dockey = str(docurl)
        imgkey = str(imgurl)
        for ent in jsonstring:
            docurl = ent[dockey]
            imageurl = ent[imgkey]
            if not (docurl in self.docurls):
                self.docurls.append(docurl)
                news = self._getNewDetail(docurl)
                if news != None:
                    news['imgurl'] = imageurl
                    newsList.append(news)
        return newsList


    def insertNewsData(self, news):
        # 存数据库
        db = pymysql.connect("rdsa2lh5500j9mlpmorzo.mysql.rds.aliyuncs.com", "jieyaozu", "yuanwang44", "superplan",
                             charset="utf8")
        # 使用 cursor() 方法创建一个游标对象 cursor
        cursor = db.cursor()
        dt = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        # SQL 插入语句
        sql = "INSERT INTO news(title, \
               content, sourcefrom, imgurl, docurl,time,classify) \
               VALUES ('%s', '%s', '%s','%s', '%s','%s','%s')" % \
              (news['title'], pymysql.escape_string(news['content']), news['sourcefrom'], news['imgurl'],news['docurl'],dt,news['classify'])
        try:
            # 执行SQL语句
            cursor.execute(sql)
            # 执行sql语句
            db.commit()
        except:
            print("!!!!!1插入数据库出错了!!!!!")
            db.rollback()
        # 关闭数据库连接
        db.close()


    def findNewsData(self, docurl):
        db = pymysql.connect("rdsa2lh5500j9mlpmorzo.mysql.rds.aliyuncs.com", "jieyaozu", "yuanwang44", "superplan",
                             charset="utf8")
        # 使用 cursor() 方法创建一个游标对象 cursor
        cursor = db.cursor()
        sql = "select * from news where docurl='%s'" % docurl
        try:
            # 执行SQL语句
            cursor.execute(sql)
            # 获取所有记录列表
            results = cursor.fetchall()
        except:
            print("查询数据出错了!")
            db.close()
            return None
        db.close()
        return results
